package cn.zysheep.order.service;


import cn.zysheep.feign.clients.UserClient;
import cn.zysheep.order.mapper.OrderMapper;


import cn.zysheep.order.pojo.Order;
import cn.zysheep.feign.pojo.User;
//import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
//import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

@Service
//@DefaultProperties(defaultFallback = "orderFallBack")
public class OrderService {

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private UserClient userClient;

    @Autowired
    private RestTemplate restTemplate;

    //@HystrixCommand(fallbackMethod = "orderFallBack")
    //@HystrixCommand
    public Order queryOrderById(Long orderId) {

        // 1.查询订单
        Order order = orderMapper.findById(orderId);

        // 发送rest http请求,获取user对象
        // String url = "http://user-service/user/"+order.getUserId();
        // User user = restTemplate.getForObject(url, User.class);

        // 利用Feign发起http请求，查询用户
        User user = userClient.queryById(order.getUserId());
        // 保存到Order订单对象中
        order.setUser(user);
        // 4.返回
        return order;
    }

    /**
     * 1、因为熔断的降级逻辑方法必须跟正常逻辑方法保证：相同的参数列表和返回值声明。
     * 2、在 queryOrderById方法上 HystrixCommand(fallbackMethod = "orderFallBack") 用来声明一个降级逻辑的方法
     * @param orderId
     * @return
     */
    // 降级方法
//    public Order orderFallBack(){
//        Order order = new Order();
//        order.setId(-1L);
//        order.setName("熔断:触发降级方法");
//        return order;
//    }
}
